$(function () {
//    var init = true; //第一次加载树之后点击根节点
//    //加载树
//    $("#west_qykhh_qyjg_tree").tree({
//        onClick: function (node) {
//            qykhh_search(node.id);
//        },
//        //新增修改删除数据后重新加载，并选中之前的节点
//        onLoadSuccess: function (node, data) {
//            var rootNode = $("#west_qykhh_qyjg_tree").tree("getRoot");
//            $("#west_qykhh_qyjg_tree").tree("select", rootNode.target);
//            if (init) {
//                rootNode.target.click();
//                init = false;
//            }
//        }
//    });

    $("#qykhh_new").click(function () {
        qykhh_new();
    });
    $("#qykhh_mod").click(function () {
        qykhh_modify();
    });
    $("#qykhh_del").click(function () {
        qykhh_delete();
    });

    $("#btn_qykhh_search").click(function () {
        qykhh_search();
    });

});

//查询
function qykhh_search() {
    $('#qykhh_grid').datagrid('load',
        {
            qyjgjc:$('#_qyjg').attr("_text") == null ? null : $('#_qyjg').attr("_text"),
            qc:$('#_qc').val(),
            jc:$('#_jc').val()
        }
    );
}

var qykhhWin;
//新增企业开户行
function qykhh_new() {
    //var node = $("#west_qykhh_qyjg_tree").tree("getSelected");
    qykhhWin = $.window({
        title: '添加开户行',
        width: 750,
        height: 450,
        cache: false,
        url: $.webPath() + '/organ/qykhh/qykhh_content',
        modal: true,
        buttons: [
            {
                text: '保存',
                handler: qykhh_saveItem
            }
        ]
    });
}
//修改企业开户行弹窗
function qykhh_modify() {
    var qykhhId = $("#qykhh_grid").datagrid("getSelected").id;
    qykhhWin = $.window({
        title: '修改开户行',
        width: 750,
        height: 450,
        cache: false,
        url: $.webPath() + '/organ/qykhh/qykhh_content?qykhhId=' + qykhhId,
        modal: true,
        buttons: [
            {
                text: '保存',
                handler: qykhh_updateItem
            }
        ]
    });
}
//删除企业开户行
function qykhh_delete() {
    var qykhhId = $("#qykhh_grid").datagrid("getSelected").id;
    $.messager.confirm("确认", "确定删除此开户行？", function (r) {
        if (r) {
            $.ajax({
                url: $.webPath() + "/organ/qykhh/delete_qykhh?qykhhId=" + qykhhId,
                type: "json",
                beforeSend: function () {
                    $("#qykhh_layout").mask();
                },
                success: function (data) {
                    $("#qykhh_layout").mask('hide');
                    data = $.parseJSON(data);
                    if (data.statusCode == '200') {
                        $('#qykhh_grid').datagrid('reload');
                        $.messager.show({
                            title: '提示框',
                            msg: data.message,
                            timeout: 3000,
                            showType: 'slide'
                        });
                    } else {
                        $.messager.alert('提示', data.message, 'info');
                    }
                }
            });
        }
    })
}
//保存 企业开户行
function qykhh_saveItem() {
    $(qykhhWin).mask();
    var url = $.webPath() + '/organ/qykhh/save_qykhh';
    $('#qykhhForm').form('submit', {
        url: url,
        onSubmit: function () {
            var valid = $(this).form('validate');
            if (!valid)
                $(qykhhWin).mask('hide');
            return valid;
        },
        success: function (data) {
            $(qykhhWin).mask('hide');
            var obj = eval('(' + data + ')');
            if (obj.statusCode == '200') {
                $(qykhhWin).window("close");
                $('#qykhh_grid').datagrid('reload');
                $.messager.show({
                    title: '提示框',
                    msg: obj.message,
                    timeout: 3000,
                    showType: 'slide'
                });
            } else {
                $.messager.alert('提示', obj.message, 'error');
            }
        }
    });
}
 //修改 企业开户行
function qykhh_updateItem() {
    $(qykhhWin).mask();
    var url = $.webPath() + '/organ/qykhh/update_qykhh';
    $('#qykhhForm').form('submit', {
        url: url,
        onSubmit: function () {
            var valid = $(this).form('validate');
            if (!valid)
                $(qykhhWin).mask('hide');
            return valid;
        },
        success: function (data) {
            $(qykhhWin).mask('hide');
            var obj = eval('(' + data + ')');
            if (obj.statusCode == '200') {
                $(qykhhWin).window("close");
                $('#qykhh_grid').datagrid('reload');
                $.messager.show({
                    title: '提示框',
                    msg: obj.message,
                    timeout: 3000,
                    showType: 'slide'
                });
            } else {
                $.messager.alert('提示', obj.message, 'error');
            }
        }
    });
}

//查询
$(function(){
    $('#qykhh_grid').datagrid(getQyKhhGridOption());
})

//列表加载
function getQyKhhGridOption() {
    //加载企业机构
    $("#_qyjg").qyjgzcombobox();
    var options = {
        url: $.webPath() + "/organ/qykhh/qykhhList",
        toolbar: "#qykhh_toolbar",
        fitColumns: true,
        singleSelect: true,
        pageNumber: 1,
        pagination: true,
        fit: true,
        rownumbers: true,
        onLoadSuccess: function (target) {
            $("#qykhh_del").hide();
            $("#qykhh_mod").hide();
        },
        onClickRow: function (rowIndex, rowData) {
            $("#qykhh_del").show();
            $("#qykhh_mod").show();
        },
        onDblClickCell: function (rowIndex, field, value) {
            if (field == "qyjg_jc")
                qykhh_modify();
        }
    };
    var columns = {columns: [
        [
            {field: 'qyjg_jc', title: '企业机构',align: 'center', width: 50},
            {field: 'qc', title: '帐户名称',align: 'center', width: 40},
            {field: 'jc', title: '帐户简称',align: 'center', width: 40},
            {field: 'zh', title: '帐号', align: 'center',width: 30},
            {field: 'lxdh', title: '联系人电话',align: 'center', width: 50}
        ]
    ]}
    options = $.extend(options, columns);
    return options;
}

